<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="用户ID" prop="uid">
        <el-input v-model="queryParams.uid" placeholder="请输入用户ID" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="手机号" prop="mobile">
        <el-input v-model="queryParams.mobile" placeholder="请输入手机号" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="渠道名称" prop="channelName" class="single-line-label">
        <span class="label-spacing"></span>
        <el-select v-model="queryParams.channelName" clearable placeholder="请选择渠道名称" filterable>
          <el-option v-for="(item, i) in extData.channelNameList || []" :key="i" :label="item.key"
            :value="item.key"></el-option>
        </el-select>
      </el-form-item>


      <el-form-item label="注册应用" prop="regAppCode">
        <el-select v-model="queryParams.regAppCode" style="width: 100%;" clearable placeholder="请选择应用名称">
          <el-option v-for="(item, i) in extData.appInfoList || []" :key="i" :label="item.key" :value="item.value">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="注册ip" prop="regReqIp">
        <el-input v-model="queryParams.regReqIp" placeholder="请输入注册ip" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>

      <el-form-item label="认证" prop="authFlag">
        <el-select v-model="queryParams.authFlag" clearable placeholder="请选择是否认证">
          <el-option label="未认证" value="0"></el-option>
          <el-option label="已认证" value="1"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="人脸" prop="faceFlag">
        <el-select v-model="queryParams.faceFlag" clearable placeholder="请选择是否人脸">
          <el-option label="未识别" value="0"></el-option>
          <el-option label="已校验" value="1"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="资质上传" prop="sourceFlag">
        <el-select v-model="queryParams.sourceFlag" clearable placeholder="请选择是否上传资质">
          <el-option label="未上传" value="0"></el-option>
          <el-option label="已上传" value="1"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="注册时间">
        <MobileDatePicker v-model="queryParams.dateRange" type="datetimerange" :default-time="['00:00:00', '23:59:59']"
          range-separator="至" start-placeholder="开始日期" end-placeholder="结束日期" value-format="yyyy-MM-dd HH:mm:ss"
          class="small-date-picker" />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="small" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="small" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="warning" plain icon="el-icon-edit" size="small" @click="showDialog = true"
          v-hasPermi="['member:sourceInfo:remove']">重置资质信息</el-button>
        <el-button type="primary" plain icon="el-icon-search" size="small" @click="openUpload"
          v-hasPermi="['member:sourceInfo:remove']">归属分析</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="baseinfoList">
      <el-table-column label="用户ID" align="center" prop="uid" width="170">
        <template slot-scope="scope">
          <span class="clickable-uid" @click="handleDetailsDialog(scope.row)">{{ scope.row.uid }}</span>
        </template>
      </el-table-column>
      <el-table-column label="手机号" align="center" prop="tmobile" width="120" />
      <el-table-column label="渠道名称" align="center" prop="channelName" width="162">
        <template slot-scope="scope">
          {{ scope.row.channelName }} [ {{ scope.row.channelCode }} ]
        </template>
      </el-table-column>
      <el-table-column label="注册应用" align="center" prop="regAppCode" />
      <el-table-column label="授信额度" align="center" prop="creditLimitAmount" />
      <el-table-column label="真实姓名" align="center" prop="trealname" />
      <!-- <el-table-column label="用户类型" align="center" prop="type" />
      <el-table-column label="注册类型" align="center" prop="regType" /> -->
      <el-table-column label="注册ip" align="center" prop="regReqIp" />
      <!-- <el-table-column label="用户状态" align="center" prop="status" /> -->
      <el-table-column label="是否认证" align="center" prop="authFlag">
        <template slot-scope="scope">
          <span :style="{ color: scope.row.authFlag === 0 ? 'red' : 'green' }">
            {{ scope.row.authFlag === 0 ? '未认证' : '已认证' }}
          </span>
        </template>
      </el-table-column>
      <el-table-column label="是否人脸" align="center" prop="faceFlag">
        <template slot-scope="scope">
          <span :style="{ color: scope.row.faceFlag === 0 ? 'red' : 'green' }">
            {{ scope.row.faceFlag === 0 ? '未识别' : '已校验' }}
          </span>
        </template>
      </el-table-column>
      <el-table-column label="资质上传" align="center" prop="sourceFlag">
        <template slot-scope="scope">
          <span :style="{ color: scope.row.sourceFlag === 0 ? 'red' : 'green' }">
            {{ scope.row.sourceFlag === 0 ? '未上传' : '已上传' }}
          </span>
        </template>
      </el-table-column>

      <el-table-column label="创建时间" align="center" width="150">
        <template slot-scope="scope">
          {{ formatTimestamp(scope.row.createTime) }}
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
      @pagination="getList" />

    <el-dialog title="重置用户资质" :visible="showDialog" @close="handleDialogClose" width="30%">
      <div v-if="!inputMobile">
        <label for="inputUid">用户ID:</label>
        <el-input v-model="inputUid" placeholder="请输入用户ID" style="margin-bottom: 10px;"></el-input>
      </div>
      <div v-if="!inputUid">
        <label for="inputMobile">手机号:</label>
        <el-input v-model="inputMobile" placeholder="请输入完整手机号" style="margin-bottom: 10px;"></el-input>
      </div>
      <el-checkbox-group v-model="types">
        <el-checkbox label="0">资质</el-checkbox>
        <el-checkbox label="1">认证</el-checkbox>
      </el-checkbox-group>
      <div slot="footer" class="dialog-footer">
        <el-button @click="showDialog = false">取消</el-button>
        <el-button type="primary" @click="handleDeleteAndUpdate">确认</el-button>
      </div>
    </el-dialog>



    <!-- 用户详情弹窗 -->
    <Member ref="MemberRef" />
    <uploadDialog ref="uploadDialogRef"></uploadDialog>

  </div>
</template>
<style scoped>
.clickable-uid {
  cursor: pointer;
  color: blue;
}

.small-date-picker {
  width: 350px;
}
</style>
<script>
import { listBaseinfo, getBaseinfo, delBaseinfo, addBaseinfo, updateBaseinfo } from "@/api/member/baseinfo";
import { delAndupSourceInfo } from "@/api/member/sourceInfo";
import Member from '@/views/member/memeber.vue'
import { baseInfoExtData } from "@/api/channel/baseInfo";
import dayjs from 'dayjs'
import { isMobile } from 'mobile-device-detect';
import uploadDialog from "./components/uploadDialog.vue";
export default {
  name: "Baseinfo",
  components: {
    Member,
    uploadDialog
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      showDialog: false,
      inputUid: "",
      inputMobile: "",
      uploadDialogRef: null,
      types: [],
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: !isMobile,
      // 总条数
      total: 0,
      // 用户基本信息表格数据
      baseinfoList: [],
      channelinfoList: [],
      extData: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        dateInfo: null,
        mobile: null,
        mobileMd5: null,
        channelCode: null,
        channelName: null,
        regAppCode: null,
        creditLimitAmount: null,
        username: null,
        realname: null,
        idcard: null,
        type: null,
        regType: null,
        regReqIp: null,
        status: null,
        authFlag: null,
        faceFlag: null,
        sourceFlag: null,
        createTime: null,
        startCreateTime: null,
        endCreateTime: null,
        dateRange: [`${dayjs().format('YYYY-MM-DD')} 00:00:00`, `${dayjs().format('YYYY-MM-DD')} 23:59:59`],
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {

      }
    };
  },
  mounted() {
    this.baseinfoList = this.baseinfoList.map(item => {
      return { ...item, uid: item.uid.toString() };
    });
  },
  created() {
    this.getList();
    this.getOptions();
  },
  methods: {
    dayjs,
    openUpload(){
      this.$refs.uploadDialogRef.show()
    },
    getOptions() {
      baseInfoExtData().then((response) => {
        this.extData = response.data;
      });
    },


    /**用户uid跳转 */
    async handleDetailsDialog(row) {
      this.$refs.MemberRef.show(row.uid)
    },
    // handleDeleteAndUpdate() {
    //   const uid = this.inputUid.trim();
    //   const typesString = this.types.join(",");
    //   this.showDialog = false;
    //   if (uid && this.types.length > 0) {
    //     const requestData = {
    //       uid: uid,
    //       types: typesString
    //     };
    //     delAndupSourceInfo(requestData)
    //       .then(response => {
    //         this.$message.success("重置成功");
    //         // 重新刷新列表数据
    //         this.getList();
    //       })
    //       .catch(error => {
    //         // 处理错误情况
    //         this.$message.error("重置失败：" + error.message);
    //       });
    //   } else {
    //     this.$message.warning("请输入有效的用户ID并选择类型");
    //   }
    // }

    handleDeleteAndUpdate() {
      const typesString = this.types.join(",");
      this.showDialog = false;

      // 检查是否选择了重置类型
      if (!typesString) {
        this.$message.warning("请选择重置类型");
        return;
      }

      // 根据输入的内容来决定使用哪个参数
      if (this.inputUid && !this.inputMobile) {
        // 使用用户ID进行重置
        const requestData = {
          uid: this.inputUid.trim(),
          types: typesString
        };

        delAndupSourceInfo(requestData)
          .then(response => {
            this.$message.success("根据用户ID重置资质成功");
            // 重新刷新列表数据
            this.getList();
          })
          .catch(error => {
            // 处理错误情况
            this.$message.error("重置失败：" + error.message);
          });
      } else if (this.inputMobile && !this.inputUid) {
        // 使用手机号进行重置
        const requestData = {
          mobile: this.inputMobile.trim(),
          types: typesString
        };

        delAndupSourceInfo(requestData)
          .then(response => {
            this.$message.success("根据手机号重置资质成功");
            // 重新刷新列表数据
            this.getList();
          })
          .catch(error => {
            // 处理错误情况
            this.$message.error("重置失败：" + error.message);
          });
      } else {
        this.$message.warning("请输入有效的用户ID或手机号并选择类型");
      }
    }

    ,
    handleDialogClose() {
      this.inputUid = "";
      this.inputMobile = "";
      this.types = [];
      this.showDialog = false;
    },

    /**更改创建时间格式 */
    formatTimestamp(timestamp) {
      if (!timestamp) {
        return '';
      }
      const date = new Date(timestamp);
      const formattedDate = `${date.getFullYear()}-${(date.getMonth() + 1).toString().padStart(2, '0')}-${date.getDate().toString().padStart(2, '0')} ${date.getHours().toString().padStart(2, '0')}:${date.getMinutes().toString().padStart(2, '0')}:${date.getSeconds().toString().padStart(2, '0')}`;
      return formattedDate;
    },

    /** 查询用户基本信息列表 */
    getList() {
      this.loading = true;
      const sata = { ...this.queryParams }
      if (sata.dateRange && sata.dateRange.length === 2) {
        const startDate = this.queryParams.dateRange[0];
        const endDate = this.queryParams.dateRange[1];
        sata.startCreateTime = startDate;
        sata.endCreateTime = endDate;
      }
      delete sata.dateRange
      listBaseinfo(sata).then(response => {
        this.baseinfoList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        uid: null,
        dateInfo: null,
        mobile: null,
        mobileMd5: null,
        channelCode: null,
        channelName: null,
        regAppCode: null,
        creditLimitAmount: null,
        username: null,
        realname: null,
        idcard: null,
        type: null,
        regType: null,
        regReqIp: null,
        status: null,
        authFlag: null,
        faceFlag: null,
        sourceFlag: null,
        createTime: null,
        startCreateTime: null,
        endCreateTime: null,
        updateTime: null
      };
      this.resetForm("form");
    },



    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.queryParams.dateRange = [];
      this.queryParams.uid = null;
      this.queryParams.mobile = null;
      this.queryParams.channelName = null;
      this.queryParams.regAppCode = null;
      this.queryParams.regReqIp = null;
      this.queryParams.sourceFlag = null;
      this.queryParams.authFlag = null;
      this.queryParams.faceFlag = null;
      this.handleQuery();
    },
  }
};
</script>
